<?

if ($DATA['component']['id']) {

	// list editor
	
	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value
	$_id_storage	= D_MB_STORAGE;			//	meta id value

	$_param			= 'edit';				//	ID param name in the URL, used here & @ template
	
	$_role			= 'registered';

	$s = $DATA['component']['settings'];
	$_source = (int)$s['source'];
	
	$_crit = "id_data=$_id_storage AND id_parent=$_source";
	
	if ($_source && ($a = (int)param('edit')) && ($DATA['object'] = datafetch('db_item', tb('data'), "$_crit AND id=$a"))) {

		if ($temp = unserialize($DATA['object']['value']))
			$DATA['object'] = array_merge($DATA['object'], array('name' => $temp['name'], 'e-mail' => $temp['e-mail'], 'data' => $temp['data']));

		$DATA['object']['date_visible'] = format_simple_date( $DATA['object']['date_create'] );
//dbdebug();
		$DATA['object']['auth'] = datafetch('db_item', tb('auth'), "login='{$DATA[object]['e-mail']}'");
//debug($DATA['object']);

	
		switch (action(array('user_create', 'user_enable', 'user_disable', 'user_kill'))) {
		
			case 'user_create':

				if ($DATA['object']['name'] && $DATA['object']['e-mail']) {
					
					$role = datafetch('db_item', tb('sec_role'), "name='$_role'");
					
					$pass = "";	$l = strlen(PASS_CHARS)-1;
					for ($i = 0; $i < PASS_LENGTH; $i++) $pass .= substr(PASS_CHARS, rand(0, $l), 1);
					
					$_record = array('login' => $DATA['object']['e-mail'], 'pass' => pwd($pass), 'attr' => 0, 'email' => $DATA['object']['e-mail'], 'name' => $DATA['object']['name']);
					
					$u = datafetch('db_insert', tb('auth'), $_record);
					
					dataset('db_delete', tb('sec_sess'), "id_user=$u");
					
					if ($role)
						dataset('db_insert', tb('sec_sess'), array('id_role' => $role['id'], 'id_user' => $u));
						
						
					$subject = '=?utf-8?B?'.base64_encode( stripslashes($s['credentials_subject'])).'?=';
		
					$random_hash = md5(date('r', time())); 
		
					$headers = "From: $s[from]";

//					$headers .= "\r\nReply-To: \"=?utf-8?B?".base64_encode( stripslashes($DATA['object']['e-mail']))."?=\" <{$DATA[object]['e-mail']}>";
		
					$headers .= "\r\nX-Mailer: PHP/".phpversion();
					$headers .= "\r\nMIME-Version: 1.0";
					$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"mixed-".$random_hash."\"";
					$headers .= "\r\nContent-Transfer-Encoding: base64";

					$body = str_replace('{pass}', $pass, $s['credentials_letter']);
					$body = chunk_split(base64_encode(stripslashes($body)));


					ob_start();
?>
--mixed-<?=$random_hash?>

Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

<?=$body?>

--mixed-<?=$random_hash?>--

<?

					$body = ob_get_clean();
					
					if (@mail($DATA['object']['e-mail'], $subject, $body, $headers))
						diags('<p class="success">Письмо с паролем отправлено пользователю</p>');
					else
						diags('<p class="error">Произошла ошибка. Письмо с паролем не было отправлено пользователю</p>');
			
				} else {
					diags('<p class="error">При регистрации пользователь не указал имя или e-mail. Без этих параметров невозможно допустить пользователя в систему.</p>');
				}
				
				
				$DATA['object']['auth'] = datafetch('db_item', tb('auth'), "login='{$DATA[object]['e-mail']}'");

				break;

			case 'user_enable':

				if ($DATA['object']['auth'])
					dataset('db_update', tb('auth'), array('attr' => $DATA['object']['auth']['attr'] | U_ENABLED), "id={$DATA[object][auth][id]}");

				header("Location: .\n");
				exit;

			case 'user_disable':

				if ($DATA['object']['auth'])
					dataset('db_update', tb('auth'), array('attr' => $DATA['object']['auth']['attr'] & ~U_ENABLED), "id={$DATA[object][auth][id]}");

				header("Location: .\n");
				exit;

			case 'user_kill':

				if ($DATA['object']['auth']) {
					dataset('db_delete', tb('sec_sess'), "id_user={$DATA[object][auth][id]}");
					dataset('db_delete', tb('auth'), "id={$DATA[object][auth][id]}");
				}

				header("Location: .\n");
				exit;
		
				break;

		}
	
	

	}
	


	return false;
	
} else exit;